<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    class="flex"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                <div style="width:100%">
                    <el-form-item label="有效性" prop="VALIDORNOT" >
                        <el-switch
                            v-model="baTable.form.items!.VALIDORNOT"
                            style="--el-switch-on-color: #13ce66;"
                            active-value="1"
                            inactive-value="0"
                        />
                    </el-form-item>
                </div>
                <FormItem :label="t('linkman.LINKMAN')" type="string" v-model="baTable.form.items!.LINKMAN" prop="LINKMAN" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.LINKMAN') }) }" />
                <FormItem :label="t('linkman.CORP')" type="string" v-model="baTable.form.items!.CORP" prop="CORP" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.CORP') }),disabled:true}" />
                <FormItem :label="t('linkman.PHONE')" type="string" v-model="baTable.form.items!.PHONE" prop="PHONE" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.PHONE') }) }" />
                <el-form-item :label="t('linkman.SEX')" prop="SEX" >
                    <el-radio-group v-model="baTable.form.items!.SEX">
                        <el-radio label="男" />
                        <el-radio label="女" />
                    </el-radio-group>              
                </el-form-item>
                <FormItem :label="t('linkman.CELLPHONE')" type="string" v-model="baTable.form.items!.CELLPHONE" prop="CELLPHONE" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.CELLPHONE') }) }" />
                <FormItem :label="t('linkman.FAX')" type="string" v-model="baTable.form.items!.FAX" prop="FAX" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.FAX') }) }" />
                <FormItem :label="t('linkman.DUTY')" type="string" v-model="baTable.form.items!.DUTY" prop="DUTY" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.DUTY') }) }" />
                <FormItem :label="t('linkman.POSTCODE')" type="string" v-model="baTable.form.items!.POSTCODE" prop="POSTCODE" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.POSTCODE') }) }" />
                <FormItem :label="t('linkman.EMAIL')" type="string" v-model="baTable.form.items!.EMAIL" prop="EMAIL" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.EMAIL') }) }" />
                <FormItem :label="t('linkman.BP')" type="string" v-model="baTable.form.items!.BP" prop="BP" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.BP') }) }" />
                <FormItem style="width:100%" :label="t('linkman.ADDRESS')" type="string" v-model="baTable.form.items!.ADDRESS" prop="ADDRESS" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.ADDRESS') }) }" />
                <!-- <FormItem :label="t('linkman.VALIDORNOT')" type="string" v-model="baTable.form.items!.VALIDORNOT" prop="VALIDORNOT" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.VALIDORNOT') }) }" /> -->
                <FormItem style="width:100%" :label="t('linkman.REMARK')" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('linkman.REMARK') }) }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" />
                <!-- <FormItem :label="t('linkman.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: t('Please input field', { field: t('linkman.OP') }) }" /> -->
                <!-- <FormItem :label="t('linkman.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: t('Please select field', { field: t('linkman.OPTIME') }) }" /> -->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject ,onMounted, watchEffect} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { mount } from 'sortablejs'
import { useRoute } from 'vue-router'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass


const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    LINKMAN: [buildValidatorData({ name: 'required', title: t('linkman.LINKMAN') })],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('linkman.OPTIME') })})],
})


const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

const companyName = inject('companyName') as String
watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.CORP = companyName.value
        baTable.form.items!.VALIDORNOT = '1'
    }
})

const handleSave= ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}


</script>

<style scoped lang="scss"></style>
